package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.BasicEquipmentTypeEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.AirMonitorEquipmentService;
import com.iwomy.envpro.platform.service.BasicEquipmentTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备类型
 *
 * @author pig
 * @date 2023-09-27 09:49:09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicEquipmentType")
@Tag(description = "basicEquipmentType", name = "设备类型管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicEquipmentTypeController {

	private final BasicEquipmentTypeService basicEquipmentTypeService;

	private final AirMonitorEquipmentService airMonitorEquipmentService;


	/**
	 * 分页查询
	 *
	 * @param page               分页对象
	 * @param basicEquipmentType 设备类型
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicEquipmentType_view')")
	public R getBasicEquipmentTypePage(@ParameterObject Page page, @ParameterObject BasicEquipmentTypeEntity basicEquipmentType) {
		LambdaQueryWrapper<BasicEquipmentTypeEntity> wrapper = Wrappers.lambdaQuery(basicEquipmentType);
		return R.ok(basicEquipmentTypeService.page(page, wrapper));
	}


	/**
	 * 通过id查询设备类型
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicEquipmentType_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicEquipmentTypeService.getById(id));
	}

	/**
	 * 新增设备类型
	 *
	 * @param basicEquipmentType 设备类型
	 * @return R
	 */
	@Operation(summary = "新增设备类型", description = "新增设备类型")
	@SysLog("新增设备类型")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicEquipmentType_add')")
	public R save(@RequestBody BasicEquipmentTypeEntity basicEquipmentType) {
		BasicEquipmentTypeEntity wrapperEntity = new BasicEquipmentTypeEntity();
		wrapperEntity.setTypeName(basicEquipmentType.getTypeName());
		LambdaQueryWrapper<BasicEquipmentTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicEquipmentTypeEntity one = basicEquipmentTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
			throw new PlatformException("设备类型已存在");
		}
		return R.ok(basicEquipmentTypeService.save(basicEquipmentType));
	}

	/**
	 * 修改设备类型
	 *
	 * @param basicEquipmentType 设备类型
	 * @return R
	 */
	@Operation(summary = "修改设备类型", description = "修改设备类型")
	@SysLog("修改设备类型")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicEquipmentType_edit')")
	public R updateById(@RequestBody BasicEquipmentTypeEntity basicEquipmentType) {


		LambdaQueryWrapper<AirMonitorEquipmentEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(AirMonitorEquipmentEntity::getEquipmentTypeId, basicEquipmentType.getId());
		long count = airMonitorEquipmentService.count(queryWrapper);
		if (count > 0) {
			return R.failed("设备类型关联了设备，不允许编辑");
		}

		BasicEquipmentTypeEntity wrapperEntity = new BasicEquipmentTypeEntity();
		wrapperEntity.setTypeName(basicEquipmentType.getTypeName());
		LambdaQueryWrapper<BasicEquipmentTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicEquipmentTypeEntity one = basicEquipmentTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(basicEquipmentType.getId())) {
			throw new PlatformException("设备类型已存在");
		}
		return R.ok(basicEquipmentTypeService.updateById(basicEquipmentType));
	}

	/**
	 * 通过id删除设备类型
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除设备类型", description = "通过id删除设备类型")
	@SysLog("通过id删除设备类型")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicEquipmentType_del')")
	public R removeById(@RequestBody Long[] ids) {

		long count = airMonitorEquipmentService.count(Wrappers.<AirMonitorEquipmentEntity>lambdaQuery().in(AirMonitorEquipmentEntity::getEquipmentTypeId, ids));

		if (count > 0)
			return R.failed("数据模板关联了设备，不允许删除");
		return R.ok(basicEquipmentTypeService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicEquipmentType 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicEquipmentType_export')")
	public List<BasicEquipmentTypeEntity> export(BasicEquipmentTypeEntity basicEquipmentType) {
		return basicEquipmentTypeService.list(Wrappers.query(basicEquipmentType));
	}

	/**
	 * @param basicEquipmentType 查询条件
	 */
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_basicEquipmentType_export')")
	public R list(BasicEquipmentTypeEntity basicEquipmentType) {
		return R.ok(basicEquipmentTypeService.list(Wrappers.query(basicEquipmentType)));
	}
}